home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / asm32.zip / DPMI32.ASM < prev    next >
Assembly Source File  |  1995-10-13  |  4KB  |  246 lines

  1. ; DPMI32.ASM for ASM32/DPMI - Copyright (C) 1995 Douglas Herr
  2. ;  all rights reserved
  3.  
  4. include    model.inc
  5.  
  6. IFNDEF    DPMI
  7. DPMI    equ    1
  8. ENDIF
  9.  
  10. public    GetMemNear, RelMemNear, ResMemNear, GetSelDet32
  11. public    GetMemLinear32, RelMemLinear32, ResMemLinear32
  12. public    Linear2Near, Near2Linear
  13.  
  14. include    dataseg.inc
  15. extrn    zero_sel:word
  16. @curseg    ends
  17.  
  18. include    codeseg.inc
  19.  
  20. ; GetMemNear
  21. ;
  22. ; In:  EBX = bytes
  23. ; Out: ESI = near address of block
  24.  
  25. GetMemNear:
  26.     push    ecx
  27.     mov    ecx,ebx            ; bytes
  28.     call    GetMemLinear32        ; ret: ESI = linear address
  29.     jc    short GetMemNearExit
  30.     call    Linear2Near        ; ret: ESI = near address
  31.  
  32. GetMemNearExit:
  33.     pop    ecx
  34.     ret
  35.  
  36.  
  37.  
  38. ; RelMemNear
  39. ;
  40. ; release memory allocated by GetMemNear
  41. ; ESI = near memory address
  42.  
  43. RelMemNear:
  44.     pushad
  45.  
  46.     call    Near2Linear
  47.     jc    short RelMemNearExit
  48.     call    RelMemLinear32
  49.  
  50. RelMemNearExit:
  51.     popad
  52.     ret
  53.  
  54.  
  55.  
  56. ; ResMemNear
  57. ;
  58. ; In:  ESI = existing near base address
  59. ;      ECX = new size requested
  60. ; Out: ESI = new base address
  61.  
  62. ResMemNear:
  63.     call    Near2Linear        ; convert to linear address
  64.     jc    short ResMemNearExit
  65.     call    ResMemLinear32        ; resize
  66.     jc    short ResMemNearExit
  67.     call    Linear2Near        ; convert to near address
  68. ResMemNearExit:
  69.     ret
  70.  
  71.  
  72. ; ResMemLinear32
  73. ;
  74. ; In:  ESI = linear address of memory block
  75. ;      ECX = new block size
  76. ; Out: [CF=0] ESI = new block address
  77. ;      [CF=1] AX = error code
  78.  
  79. ResMemLinear32:
  80.     push    ebx
  81.     push    ecx
  82.     push    edx
  83.     push    edi
  84.     push    es
  85.  
  86.     mov    es,zero_sel
  87.     mov    esi,es:[esi-4]        ; get handle
  88.     movzx    edi,si
  89.     shr    esi,16            ; SI:DI = block handle
  90.  
  91.     add    ecx,4            ; allow space for handle storage
  92.     mov    ebx,ecx
  93.     movzx    ecx,cx
  94.     shr    ebx,16            ; BX:CX = bytes
  95.  
  96.     mov    ax,0503h        ; resize linear memory
  97.     int    31h
  98.     jc    short ResMemLinear32Exit
  99.  
  100.     shl    esi,16
  101.     mov    si,di            ; ESI = handle
  102.  
  103.     shl    ebx,16
  104.     mov    bx,cx            ; EBX = new address of block
  105.     mov    es:[ebx],esi
  106.     add    ebx,4
  107.     mov    esi,ebx            ; new block address
  108.     clc                ; no error
  109.  
  110. ResMemLinear32Exit:
  111.     pop    es
  112.     pop    edi
  113.     pop    edx
  114.     pop    ecx
  115.     pop    ebx
  116.     ret
  117.  
  118.  
  119.  
  120. ; Linear2Near
  121. ;
  122. ; In:  ESI = linear address
  123. ; Out: ESI = near address
  124.  
  125. Linear2Near:
  126.     pushad
  127.  
  128.     mov    bx,ds
  129.     call    GetSelDet32        ; ret: EDX = selector base address
  130.     jc    short Linear2NearExit
  131.  
  132.     mov    ebx,esi            ; linear address
  133.     sub    ebx,edx
  134.     jc    short Linear2NearExit
  135.     mov    esi,ebx
  136.  
  137. Linear2NearExit:
  138.     mov    StackESI,esi
  139.     popad
  140.     ret
  141.  
  142.  
  143. ; Near2Linear
  144. ;
  145. ; In:  ESI = near address
  146. ; Out: ESI = linear address
  147.  
  148. Near2Linear:
  149.     pushad
  150.  
  151.     mov    bx,ds
  152.     call    GetSelDet32        ; ret: EDX = selector base address
  153.     jc    short Near2LinearExit
  154.     add    esi,edx            ; add base to near -> linear
  155.  
  156. Near2LinearExit:
  157.     mov    StackESI,esi
  158.     popad
  159.     ret
  160.  
  161.  
  162. ; GetMemLinear
  163. ;
  164. ; In:  ECX = bytes
  165. ; Out: ESI = linear address
  166.  
  167. GetMemLinear32:
  168.     pushad
  169.     push    es
  170.  
  171.     add    ecx,4        ; allow space for handle storage
  172.     mov    ebx,ecx
  173.     shr    ebx,16        ; BX:CX = bytes
  174.     mov    ax,0501h
  175.     int    31h
  176.     jc    short GetMemLinear32Exit
  177.  
  178.     shl    ebx,16
  179.     mov    bx,cx        ; EBX = linear address
  180.  
  181.     shl    esi,16
  182.     mov    si,di        ; ESI = handle
  183.  
  184.     mov    es,zero_sel
  185.     mov    es:[ebx],esi
  186.  
  187.     add    ebx,4
  188.     mov    esi,ebx        ; user address
  189.  
  190. GetMemLinear32Exit:
  191.     pop    es
  192.     mov    StackESI,esi
  193.     popad
  194.     ret
  195.  
  196.  
  197. ; RelMemLinear32
  198. ;
  199. ; In:  ESI = linear address of memory block
  200.  
  201. RelMemLinear32:
  202.     pushad
  203.     push    es
  204.  
  205.     mov    es,zero_sel
  206.     mov    esi,es:[esi-4]    ; get handle
  207.     movzx    edi,si
  208.     shr    esi,16        ; SI:DI = handle
  209.     mov    ax,0502h
  210.     int    31h
  211.  
  212.     pop    es    
  213.     popad
  214.     ret
  215.  
  216.  
  217.  
  218. ; GetSelDet32
  219. ;
  220. ; In:  BX = selector
  221. ; Out: EDX = linear base address
  222. ;      ECX = selector byte limit
  223.  
  224. .386p
  225.  
  226. GetSelDet32:
  227.     push    eax
  228.     push    ebx
  229.     mov    eax,0006h
  230.     int    31h
  231.     jc    short exitDet32
  232.     movzx    edx,dx
  233.     shl    ecx,16
  234.     or    edx,ecx        ; EDX = linear base address
  235.     movzx    ebx,bx
  236.     lsl    ecx,ebx        ; ECX = byte limit
  237.     clc
  238.  
  239. exitDet32:
  240.     pop    ebx
  241.     pop    eax
  242.     ret
  243.  
  244. @curseg    ends
  245.     end
  246.